package com.xy.p2p.action.admin;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xy.common.util.DictUtils;
import com.xy.mail.MailSenderInfo;
import com.xy.mail.SimpleMailSender;
import com.xy.p2p.model.Tfinanceinfo;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberbankcard;
import com.xy.p2p.model.Tmembercredit;
import com.xy.p2p.model.Tmemberrates;
import com.xy.p2p.service.TfinanceinfoService;
import com.xy.p2p.service.TmemberService;
import com.xy.p2p.service.TmemberbankcardService;
import com.xy.p2p.service.TmembercreditService;
import com.xy.p2p.service.TmemberratesService;

@Controller
public class MailAction {
	@RequestMapping("v_sendmail.do")
	public String mail(long fiid){
		//设置两位小数点
		DecimalFormat df=new DecimalFormat(".##");
		//设置返回日期的格式
		SimpleDateFormat dateformatAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当前标的ID
		Tfinanceinfo tfinanceinfo = tfinanceinfoService.get(fiid);
		//获取memberid
		Tmember tmember = tfinanceinfo.getTmember();
		//设置其他表的memberid字段
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("Tmember", tmember);
		//获取对应mamberid的会员信用
		Tmembercredit tmembercredit = tmembercreditService.getTmembercredit(map).get(0);
		//设置提现银行
		map.put("iswithdraw", 1);
		//获取对应mamberid的会员银行卡
		Tmemberbankcard tmemberbankcard = tmemberbankcardService.getTmemberbankcard(map).get(0);
		Map<String, Object> map2 = new HashMap<String, Object>();
		//设置信用对应的费率
		map2.put("feedcode", Tmemberrates.PRE_RATE_SERVICE + tmembercredit.getCreditrating());
		//获取费率
		Tmemberrates tmemberrates = tmemberratesService.getTmemberrates(map2).get(0);
		//获取充值手续费
		Tmemberrates tmemberrates1 = tmemberratesService.getFee(Tmemberrates.RECHARGE_FEE);
		//要发送的邮箱
		String email = tmember.getEmail();
		//要发送的标题
		String name = tmember.getTruename();
		//下一还款金额
		double next = tfinanceinfo.getSubpri()+tfinanceinfo.getAlino()+tfinanceinfo.getMftba();
		//会员要充值多少钱
		double chong = next+next*tmemberrates1.getRates()*(1+tmemberrates1.getRates());
		String chongzhi = df.format(chong);
		//百分比的充值手续费
		String baifenbi = String.format("%.2f", tmemberrates1.getRates()*100);
		//发送的内容
		String content = name + ",你好!" + "\n        " + "你申请的融资标的（融资标的ID是:" + tfinanceinfo.getFinancekey() + 
				"，融资金额" + tfinanceinfo.getFinanceamount() + "元，融资期限" + tfinanceinfo.getFinanceallotedtime()/30 + "个月，年利率" + 
				tfinanceinfo.getFinancepa()*100 + "%）已满标，贷一贷平台已放款到你提现账户上（银行名称：" 
				+ tmemberbankcard.getBank() + "，银行卡号：" + tmemberbankcard.getBankcard() + 
				"），放款金额是" + (tfinanceinfo.getFinanceamount()-tfinanceinfo.getFinanceamount()*tmemberrates.getRates()) + 
				"元。该融资标的还款方式: " + DictUtils.getDictByType(DictUtils.TFINANCEINFO_REPAYMENTMOD).get(tfinanceinfo.getRepaymentmod().toString()) + "，下一还款日期：" + 
				dateformatAll.format(tfinanceinfo.getNrdate()) + "，下一还款金额：" + next + 
				"元（其中待还本金：" + tfinanceinfo.getSubpri() + "元， 待还利息：" + tfinanceinfo.getAlino() + 
				"元， 待还管理费：" + tfinanceinfo.getMftba() + "元（借款管理费费率：" + tfinanceinfo.getMfrl()*100 + 
				"%，按期收取，" + tfinanceinfo.getMftba() + "=" +tfinanceinfo.getFinanceamount() + "*" + tfinanceinfo.getMfrl()*100 + 
				"%）），请按时足额还款，维持良好信用。" + "\n        " + 
				"还款提示：上贷一贷www.51d1d.com，登录你的账户，“资金管理”-“充值”，进入充值界面，选择银行、填写充值金额，充值时第三方支付平台要收取充值金额的" + 
				baifenbi + "%作为充值手续费，你下次还款时建议你充值" + chongzhi + "，即" + chongzhi +"= " + 
				next + '+' + next + '*' + baifenbi + "%*（1+" + tmemberrates1.getRates() +"）。";
		//发送邮件
		sendmail(email,name,content);
		
		return "text"; 
	}
	
	public void sendmail(String email,String name,String content){
		//这个类主要是设置邮件
		MailSenderInfo mailInfo = new MailSenderInfo();    
	     mailInfo.setMailServerHost("smtp.ym.163.com");   
	     mailInfo.setMailServerPort("25");
	     mailInfo.setValidate(true);
	     mailInfo.setUserName("d1d@51d1d.com");    
	     mailInfo.setPassword("d1d85698359");//您的邮箱密码    
	     mailInfo.setFromAddress("d1d@51d1d.com"); 
	     mailInfo.setToAddress(email);    
	     mailInfo.setSubject(name);    
	     mailInfo.setContent(content);
        //这个类主要来发送邮件
     SimpleMailSender sms = new SimpleMailSender();   
         sms.sendTextMail(mailInfo);//发送文体格式
	}
	@Resource
	private TmemberService tmemberService;
	@Resource
	private TmemberratesService tmemberratesService;
	@Resource
	private TfinanceinfoService tfinanceinfoService;
	@Resource
	private TmemberbankcardService tmemberbankcardService;
	@Resource
	private TmembercreditService tmembercreditService;
	
}
